package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.model.ProductPool;
import com.yami.shop.service.ProductDockingService;
import com.yami.shop.service.ProductPoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 商品池Controller
 */
@RestController
@RequestMapping("/product/pool")
@Api(tags = "商品池接口")
@Slf4j
public class ProductPoolController {

    @Autowired
    private ProductPoolService productPoolService;



    @GetMapping("/page")
    @ApiOperation(value = "分页查询商品池列表")
    public ResponseEntity<IPage<ProductPool>> pageProductPool(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("店铺ID") @RequestParam(required = false) Long shopId,
            @ApiParam("商品池名称") @RequestParam(required = false) String poolName) {
        try {
            Page<ProductPool> page = new Page<>(pageNum, pageSize);
            IPage<ProductPool> result = productPoolService.pageProductPool(page, shopId, poolName);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("分页查询商品池列表失败", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "获取商品池详情")
    public ResponseEntity<ProductPool> getProductPool(@PathVariable("id") Long id) {
        try {
            ProductPool productPool = productPoolService.getProductPoolById(id);
            return ResponseEntity.ok(productPool);
        } catch (Exception e) {
            log.error("获取商品池详情失败", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增商品池")
    public ResponseEntity<Void> saveProductPool(@Valid @RequestBody ProductPool productPool) {
        try {
            productPoolService.saveProductPool(productPool);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            log.error("新增商品池失败", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新商品池")
    public ResponseEntity<Void> updateProductPool(@Valid @RequestBody ProductPool productPool) {
        try {
            productPoolService.updateProductPool(productPool);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            log.error("更新商品池失败", e);
            return ResponseEntity.badRequest().build();
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除商品池")
    public ResponseEntity<Void> deleteProductPool(@PathVariable("id") Long id) {
        try {
            productPoolService.deleteProductPool(id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            log.error("删除商品池失败", e);
            return ResponseEntity.badRequest().build();
        }
    }
} 